using UnityEngine;
using System.Collections;

public class ArrowBehaviour : MonoBehaviour 
{
    private Transform characterTransform;
    public float speed;

    void Start()
    {
        characterTransform = GameObject.FindWithTag("Player").transform;

        rigidbody.velocity = CalculateVel(characterTransform) * speed;
        Destroy(gameObject, 10);
    }

    Vector3 CalculateVel(Transform target) 
    {
	    var dir = target.position - transform.position; 
	    var h = dir.y; 
	    dir.z = 0; 
	    var dist = dir.magnitude ; 
	    dir.z = dist; 
	    dist += h; 
	    var vel = Mathf.Sqrt(dist * Physics.gravity.magnitude);
	    return vel * dir.normalized;
    }

    void OnTriggerStay(Collider other)
    {
        if (other.gameObject.tag == "Player")
        {
            if (other.gameObject.GetComponent<CharMovement>().IsAttacking() == false)
            {
                other.gameObject.GetComponent<HP>().Damage(1);
            }

            Destroy(gameObject);
        }
    }
}